package com.zhong.gang.pda.utils;

import android.app.Activity;
import android.os.Handler;
import android.os.Trace;
import android.util.Log;
import android.zyapi.CommonApi;
import android.zyapi.Conversion;

import java.io.File;
import java.util.Vector;

public class PowerUtilNew {

    private CommonApi commonApi;
    private boolean isOpen;
    private final int MAX_RECV_BUF_SIZE = 512;

    private int mComFd = -1;
    private String strRead;
    private String[] seral = null;
    private Activity activity;


    private PowerUtilListener listener;

    private byte[] recv;

    public PowerUtilNew(Activity activity, PowerUtilListener listener) {
        this.activity = activity;
        this.listener = listener;
    }

        public interface PowerUtilListener {
        void onDataReceived(String str);
    }


    /**
     * 获取串口设备名称
     */
    public void getDevices() {
        Vector<File> mDevices = null;
        if (mDevices == null) {
            mDevices = new Vector<File>();
            File dev = new File("/dev");
            File[] files = dev.listFiles();
//			for (int i = 0; i < files.length; i++) {
//				if (files[i].getAbsolutePath().startsWith("/dev/ttyM")||files[i].getAbsolutePath().startsWith("/dev/ttyG")) {
            mDevices.add(new File("/dev/ttyMT0"));
            mDevices.add(new File("/dev/ttyMT1"));
            mDevices.add(new File("/dev/ttyMT2"));
            mDevices.add(new File("/dev/ttyMT3"));
            mDevices.add(new File("/dev/ttyMT4"));
//				}
//			}
        }
        for (int i = 0; i < mDevices.size(); i++) {
            if (seral == null) {
                seral = new String[mDevices.size()];
            }
            seral[i] = mDevices.get(i).toString();
        }
    }

    /**
     * 设置串口
     */
    public void setGpio() {
        if (commonApi == null) {
            commonApi = new CommonApi();
        }
        commonApi.setGpioDir(27, 0);
        commonApi.getGpioIn(27);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                commonApi.setGpioMode(27, 0);
                commonApi.setGpioDir(27, 1);
                int ret = commonApi.setGpioOut(27, 1);
                if (ret == 0) {
                    Log.d("commonApi", "commonApi_ret:" + ret);
                } else {
                    Log.d("commonApi", "commonApi_retError:" + ret);
                }
            }
        },1000);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                open();
            }
        },3000);

    }

    /**
     * 打开
     *
     * @return
     */
    public boolean open() {
        if (commonApi == null) {
            commonApi = new CommonApi();
        }
        mComFd = commonApi.openCom("/dev/ttyS1", 9600, 8, 'N', 1);

        if (mComFd > 0) {
            isOpen = true;
            send(new byte[]{(byte) 0xaa});
            readData();
            return true;
        } else {
            isOpen = false;
            return false;
        }

    }

    /**
     * 读数据线程
     */
    private void readData() {
        new Thread() {
            public void run() {
                while (isOpen) {
                    int ret = 0;
                    byte[] buf = new byte[MAX_RECV_BUF_SIZE + 1];
//                    Log.d(Tag, "mComfd = " +mComFd);
                    ret = commonApi.readComEx(mComFd, buf, MAX_RECV_BUF_SIZE, 0, 0);
                    if (ret <= 0) {
//                        Log.e(Tag,"read failed!!!! ret:"+ret);
                        try {
                            sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        continue;
                    }
                    recv = new byte[ret];
                    System.arraycopy(buf, 0, recv, 0, ret);
//					if(mHexshow.isChecked()){
                    strRead = Conversion.Bytes2HexString(recv);
                    Long caNo = Long.parseLong(strRead,16);
                    Log.d("msg", strRead);



                }
            }
        }.start();
    }

    /**
     * 发送数据
     */
    private void send(byte[] data) {
        if (data == null) return;
        if (mComFd > 0) {
//			Log.d(Tag,"发送数据"+"data = "+data);
            commonApi.writeCom(mComFd, data, data.length);
        }
    }
}
